package com.itheima.article.listener;

import com.alibaba.fastjson.JSON;
import com.itheima.article.dto.ArticleVisitStreamMess;
import com.itheima.article.service.ApArticleService;
import com.itheima.common.constants.BusinessConstants;
import com.itheima.wemedia.dto.DownOrUpDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @description 实时热点文章算分-更新文章 交互行为 数量
 * @package com.itheima.article.listener
 */
@Component
@Slf4j
public class ArticleHotListener {

    @Resource
    private ApArticleService apArticleService;

    @KafkaListener(topics = BusinessConstants.MqConstants.HOT_ARTICLE_INCR_HANDLE_TOPIC)
    public void consumerMsg(ConsumerRecord<String,String> msg){
        int p = msg.partition();
        long o = msg.offset();
        String json = msg.value();
        log.info("实时热点文章算分-更新文章数量开始: {},{},{}", p,o,json);
        ArticleVisitStreamMess mess = JSON.parseObject(json, ArticleVisitStreamMess.class);
        // 更新文章数量与重新计算再存入redis
        apArticleService.computeAndSave2RedisSingle(mess);
        log.info("实时热点文章算分-更新文章数量完成: {},{},{}", p,o,json);
    }
}
